package com.gxa.saas.sso.advice;

import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@RestController
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

/*
* 将异常追加成长字符串
* */
// 处理第一种校验异常: 将异常信息拼接,返回给前端显示
    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
    public JsonResult handleBindException(Errors errors){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        StringBuilder stringBuilder = new StringBuilder();
        errors.getAllErrors().forEach(error->{
            stringBuilder.append(error.getDefaultMessage()+",");
        });
        jsonResult.setMsg(stringBuilder.toString());
        return jsonResult;
    }
// 处理第二种校验异常: 将异常信息拼接,返回给前端显示
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult handleConstrainViolationException(ConstraintViolationException exception){
        StringBuilder errorInfo = new StringBuilder();
        String errorMessage;
        Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
        for(ConstraintViolation<?> item : violations){
            errorInfo.append(item.getMessage()).append(",");
        }
        errorMessage  = errorInfo.toString().substring(0,errorInfo.toString().length()-1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(errorMessage);
        return jsonResult;
    }

    // 处理业务异常: 将异常信息返回给前端显示
    @ExceptionHandler(BusinessException.class)
    public JsonResult
    handleBusinessException(BusinessException exception) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(exception.getMessage());
        return jsonResult;
    }

    // 处理系统异常: 将异常信息返回给前端打哈哈,系统后台记录日志
    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e) {
        // 把系统异常日志记录
        log.info("-----------------begin------------------ -----"); //分割线方便阅读
        log.info(String.valueOf(e)); // 先记录异常对象的toString状态: 包括异常类型和异常原因
        // 再遍历栈中的异常轨迹: 采用遍历方式
        StackTraceElement[] stackTrace = e.getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            log.info(String.valueOf(stackTraceElement));
        }
        log.info("-------------------end------------------ ---");//分割线方便阅读
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("系统开小差了,请稍后再试");
        return jsonResult;
    }


}
